package org.example;

import java.util.ArrayList;
import java.util.List;

class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }


public class Test1 {
    //leetcode 144 二叉树的前序遍历 https://leetcode.cn/problems/binary-tree-preorder-traversal/
    /**
     * 递归的方式实现二叉树的前序遍历
     */
    List<Integer> ret;
    public List<Integer> preorderTraversal(TreeNode root) {
        ret = new ArrayList<>();
        preorder(root);
        return ret;
    }

    public void preorder(TreeNode root) {
        if (root == null) return;
        ret.add(root.val);
        preorder(root.left);
        preorder(root.right);
    }
}
